from sqlalchemy import Column, ForeignKey, Integer, String
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import backref, relation
from divirt import sqlalchemy_meta_data
from divirt import logging
from host import Host

log = logging.getLogger('divirt.components.tap')

Base = declarative_base(metadata=sqlalchemy_meta_data)
class Tap(Base):
    # Table
    __tablename__ = 'taps'
    id = Column(Integer, primary_key=True)
    host_id = Column(Integer, ForeignKey('hosts.id'))
    ifname = Column(String)
    ip_addr = Column(String)
    netmask = Column(String)
    
    # Relations
    host = relation(Host, backref=backref('taps', order_by=id))

    def __init__(self, host_id=None, ifname=None, ip_addr=None, netmask=None):
        self.host_id = host_id
        self.ifname = ifname
        self.ip_addr = ip_addr
        self.netmask = netmask
    
    def __repr__(self):
        return '<Vm(id=%s, host_id=%s, ifname=%s, ip_addr=%s, netmask=%s)>' \
            % (self.id, self.host_id, self.ifname, self.ip_addr, self.netmask)
 
